package com.stn.zjx.givingtreasure.activity.bi;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.MainFragmentAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.fragment.bimain.BiAssetsFragment;
import com.stn.zjx.givingtreasure.fragment.bimain.BiMarketFragment;
import com.stn.zjx.givingtreasure.utils.StatusBarUtil;
import com.stn.zjx.givingtreasure.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

public class BiMainActivity extends BaseActivity implements TabLayout.BaseOnTabSelectedListener {
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewpager)
    NoScrollViewPager viewpager;
    //底部菜单文字内容，集合
    @BindArray(R.array.main_bi_menu)
    String[] mainBiMenu;
    //底部菜单图片，集合
    int[] imgsMenu = new int[]{
            R.mipmap.main_assets_press, R.mipmap.main_assets_nopress,
            R.mipmap.main_market_press, R.mipmap.main_market_nopress

    };
    public BiAssetsFragment assetsFragment;
    private BiMarketFragment biMarketFragment;
    private MainFragmentAdapter mainAdapter;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        setupTabIcons();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setImmersiveStatusBar(this,false);
        ButterKnife.bind(this);
        initComponent();
    }



    //初始化底部选项文字以及图片
    public void setupTabIcons() {
        if (mainBiMenu == null || mainBiMenu.length == 0 || tablayout == null) {
            return;
        }
        int size = mainBiMenu.length;
        viewpager.setOffscreenPageLimit(mainBiMenu.length-1);
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(assetsFragment = new BiAssetsFragment());
        fragmentList.add(biMarketFragment = new BiMarketFragment());

        viewpager.setAdapter(mainAdapter = new MainFragmentAdapter(this, getSupportFragmentManager(), fragmentList));
        viewpager.setNoScroll(true);
        viewpager.setOffscreenPageLimit(1);

        for (int i = 0; i < size; i++) {
            tablayout.addTab(tablayout.newTab().setCustomView(getTabView(i)));
        }


        //监听被选中子菜单
        tablayout.addOnTabSelectedListener(this);


    }
    /**
     * 获取子菜单
     *
     * @param position 当前位置
     * @return 得到子菜单选项
     */
    //得到底部子菜单选项
    public View getTabView(int position) {
        View tabView = View.inflate(this, R.layout.item_main_tab, null);
        TextView txt_title = tabView.findViewById(R.id.txt_title);
        txt_title.setText(mainBiMenu[position]);
        ImageView img_title = tabView.findViewById(R.id.img_title);
        if (position == 0) {
            txt_title.setTextColor(getResources().getColor(R.color.colorAccent));
            img_title.setImageResource(imgsMenu[position * 2]);
        } else {
            txt_title.setTextColor(getResources().getColor(R.color.default_black));
            img_title.setImageResource(imgsMenu[position * 2 + 1]);
        }
        return tabView;
    }

    /**
     * 当被选中变化子菜单内容
     *
     * @param tab 子菜单组件
     */
    //选中时tab字体颜色和icon
    private void changeTabSelect(TabLayout.Tab tab) {
        //如果不等于空
        assert tab.getCustomView() != null;
        ImageView img_title = tab.getCustomView().findViewById(R.id.img_title);
        TextView txt_title = tab.getCustomView().findViewById(R.id.txt_title);
        int position = tab.getPosition();
        txt_title.setTextColor(getResources().getColor(R.color.colorAccent));
        img_title.setImageResource(imgsMenu[position * 2]);
        viewpager.setCurrentItem(tablayout.getSelectedTabPosition(), false);
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(1,1.2f,1,1.2f,
                Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF,1.0f);
        //3秒完成动画
        scaleAnimation.setDuration(100);
        //将AlphaAnimation这个已经设置好的动画添加到 AnimationSet中
        animationSet.addAnimation(scaleAnimation);
        //启动动画
        img_title .startAnimation(animationSet);
    }

    /**
     * 没有被选中变化子菜单内容
     *
     * @param tab 子菜单组件
     */
    //默认tab字体颜色和icon
    private void changeTabNormal(TabLayout.Tab tab) {
        assert tab.getCustomView() != null;
        ImageView img_title = tab.getCustomView().findViewById(R.id.img_title);
        TextView txt_title = tab.getCustomView().findViewById(R.id.txt_title);
        int position = tab.getPosition();
        txt_title.setTextColor(getResources().getColor(R.color.default_black));
        img_title.setImageResource(imgsMenu[position * 2 + 1]);

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        changeTabSelect(tab);
        viewpager.setCurrentItem(tab.getPosition(), false);
    }

    //监听子菜单没被选中
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        changeTabNormal(tab);
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
